package com.study.bigwork.user.dao;

import java.io.Serializable;

import javax.annotation.Resource;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.study.bigwork.entity.User;

@Repository
public class UserDao {
	@Resource
	private SessionFactory sessionFactory;
	//修改拍拍币信息
	public boolean updateMoney(User user) {
		Session session = this.sessionFactory.getCurrentSession();
		String hql = "update User set clapping_money=? where id=?";
		Query query = session.createQuery(hql);
		query.setParameter(0, user.getClapping_money());
		query.setParameter(1, user.getId());
		int count = 0;
		count = query.executeUpdate();
		return count>0 ? true : false;
	}	
	//用户登录
	public  User findUserByPhoneNumberAndPassword(User user) {
		User user2 = null;
		Session session = this.sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User where phoneNumber=? and password=?");
		query.setParameter(0, user.getPhoneNumber());
		query.setParameter(1, user.getPassword());
		user2 = (User) query.uniqueResult();
		if(user2!=null) {
			return user2;
		}
		return null;
	}
	
	//更新用户信息
	public boolean updateUserInfo(User user) {
		Session session = this.sessionFactory.getCurrentSession();
		String hql = "update User set nickname=?,sex=?,phoneNumber=?,password=?,image=? where id=?";
		Query query = session.createQuery(hql);
		query.setParameter(0, user.getNickname());
		query.setParameter(1, user.getSex());
		query.setParameter(2, user.getPhoneNumber());
		query.setParameter(3, user.getPassword());
		query.setParameter(4, user.getImage());
		query.setParameter(5, user.getId());
		//同时修改朋友圈的信息
		String hql1 = "update Circle set userImg=?,userName=? where userId=?";
		Query query1 = session.createQuery(hql1);
		query1.setParameter(0, user.getImage());
		query1.setParameter(1, user.getNickname());
		query1.setParameter(2, user.getId());
		int count = 0;
		count = query.executeUpdate();
		int count1=0;
		count1 = query1.executeUpdate();
		System.out.println("count1"+count1);
		return count>0 ? true : false;	
	}
	//判断用户是否存在
	public boolean isExist(User user) {
		User user2 = null;
		Session session = this.sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User where phoneNumber=?");
		query.setParameter(0, user.getPhoneNumber());
		user2 = (User) query.uniqueResult();
		if(user2!=null) {
			return true;
		}
		return false;
	}
	//根据手机号查找用户
	public User findUserByPhoneNumber(User user) {
		User user2 = null;
		Session session = this.sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User where phoneNumber=?");
		query.setParameter(0, user.getPhoneNumber());
		user2 = (User) query.uniqueResult();
		if(user2!=null) {
			return user2;
		}
		return user2;
	}
	//添加用户
	public boolean addUser(User user) {
		Session session = this.sessionFactory.getCurrentSession();
		Serializable result = session.save(user);
		int count = (int) result;
		return count>0 ? true : false;
	}
	//根据手机号查找用户
	public User findUserById(int id) {
		User user2 = null;
		Session session = this.sessionFactory.getCurrentSession();
		Query query = session.createQuery("from User where id=?");
		query.setParameter(0, id);
		user2 = (User) query.uniqueResult();
		if(user2!=null) {
			return user2;
		}
		return user2;
	}	
	//获取用户
	public User getUser(int id) {
		
		Session session=this.sessionFactory.getCurrentSession();
		Query query =session.createQuery("from User where id=?");
		query.setParameter(0, id);
		return (User) query.uniqueResult();
	}	
	//获取chat信息
	public User getChatInfo(User user) {
		Session session=this.sessionFactory.getCurrentSession();
		Query query =session.createQuery("from User where chat_id=?");
		query.setParameter(0, user.getChat_id());
		return (User) query.uniqueResult();
	}

	/**
	 * 根据用户id找用户姓名
	 */
	public String findNameById(int id) {
		System.out.println(id);
		
		String name=null;
		String hql = "from User where id=?";
		Session session=sessionFactory.getCurrentSession();
		Query query=session.createQuery(hql);
		query.setParameter(0, id);
		User user=(User) query.uniqueResult();
		name=user.getNickname();
		return name;
	}
	
	/**
	 * 根据id获取iamge名称
	 * @param id
	 * @return
	 */
	public String findImageById(int id) {
		String image=null;
		String hql = "from User where id=?";
		Session session=sessionFactory.getCurrentSession();
		Query query=session.createQuery(hql);
		query.setParameter(0, id);
		User user=(User) query.uniqueResult();
		image=user.getImage();
		return image;
	}
}
